package com.ncloud.system.business;

import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.system.entity.Depart;
import com.ncloud.system.entity.DepartRole;
import com.ncloud.system.entity.Role;
import com.ncloud.system.entity.User;
import com.ncloud.system.service.CompanyService;
import com.ncloud.system.service.DepartService;
import com.ncloud.system.service.RoleService;
import com.ncloud.system.service.UserService;
import com.sun.tools.corba.se.idl.PragmaEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Component
public class DepartBusi {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DepartService departService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CompanyService companyService;
    @Transactional
    public void addDepart(Depart depart){
        if(companyService.getCompanyStateById(depart.getCompanyId().intValue()).intValue()>=1){
            throw new UserException(ResultEnum.COMPANY_LOCK);
        }
        /*部门*/
        Long departId = departService.insertDepart(depart);
        /*部门角色*/
        depart.getRoles().stream().forEach(role->{
            DepartRole departRole = new DepartRole();
            departRole.setDepartId(departId);
            departRole.setRoleId(role.getRoleId());
            departRole.setUseTag("1");
            departService.insertDepartRole(departRole);
        });
    }

    @Transactional
    public void modifyDepart(Depart depart){
        if(companyService.getCompanyStateById(depart.getCompanyId().intValue()).intValue()>=1){
            throw new UserException(ResultEnum.COMPANY_LOCK);
        }
        /*部门*/
        departService.updateDepart(depart);
        /*部门角色*/
        departService.deleteDepartRole(depart.getDepartId());
        depart.getRoles().stream().forEach(role->{
            DepartRole departRole = new DepartRole();
            departRole.setDepartId(depart.getDepartId());
            departRole.setRoleId(role.getRoleId());
            departRole.setUseTag("1");
            departService.insertDepartRole(departRole);
        });
    }

    @Transactional
    public void deleteDepart(Long companyId,Long departId){
        if(companyService.getCompanyStateById(companyId.intValue()).intValue()>=1){
            throw new UserException(ResultEnum.COMPANY_LOCK);
        }
        /*部门*/
        departService.deleteDepartState(departId);
        /*部门角色*/
        departService.deleteDepartRoleState(departId);
        /*删除用户*/
        userService.deleteUserByDepartId(departId);
    }
    @Transactional(readOnly= true)
    public List<Depart> getDepartInfo(Long userId){
        //这里根据用户获取用户信息，这里暂时写死
        User user = userService.findByUserId(userId);
       /*部门:如果是系统超级管理员，查询所有，否则查询自己组织*/
        List<Depart> departs = null;
        if("1".equals(user.getSuperman())){
            departs = departService.getDepartsAll();
        }else{
            Long companyId = user.getCompanyId();
            departs = departService.getDepartsByCompanyId(companyId);
        }

        /*部门角色*/
        departs.stream().forEach(depart -> {
            List<Role> departRoles = departService.getRolesByDepartId(depart.getDepartId());
             /*可以选择的部门角色*/
            List<Role> selectedRole = roleService.getRolesByCompanyId(depart.getCompanyId());
             /*已结选择的部门角色*/
            Set<Long> roles = departRoles.stream().map(Role::getRoleId).collect(Collectors.toSet());
            selectedRole.stream().forEach(role->{
                if(roles.contains(role.getRoleId())){
                    role.setSelectState(true);
                }else{
                    role.setSelectState(false);
                }
            });
            /*设置*/
            depart.setRoles(departRoles);
            depart.setRoleCount(departRoles.size());
            depart.setAccountCount(departService.getDepartAccountNum(depart.getDepartId()));//账户数
        });
        return departs;
    }

    @Transactional(readOnly= true)
    public Depart getDepartDetail(Long departId){
        /*部门*/
        Depart depart = departService.getOneDepart(departId);
        /*部门角色*/
        /*已结选择的部门角色*/
        Set<Long> roles = departService.getRolesByDepartId(departId).stream().map(Role::getRoleId).collect(Collectors.toSet());

        /*可以选择的部门角色*/
        List<Role> selectedRole = roleService.getRolesByCompanyId(depart.getCompanyId());
        selectedRole.stream().forEach(role->{
            if(roles.contains(role.getRoleId())){
                role.setSelectState(true);
            }else{
                role.setSelectState(false);
            }
        });
        depart.setRoles(selectedRole);
        return depart;
    }
}
